/**
 * Date: 2004-4-4
 * Time: 16:17:35
 */

package org.cnmarc4j.cnmarc;

import java.text.DecimalFormat;

/**
 * <p><code>Leader</code> defines behaviour for the record label
 * (record position 00-23).  </p>
 *
 * <p>The leader is a fixed field that occurs at the beginning of a
 * MARC record and provides information for the processing of the record.
 * The structure of the leader according to the MARC standard is as
 * follows:</p>
 * <pre>
 * <p>MARC21 Leader</p>
 * RECORD_LENGTH RECORD_STATUS TYPE_OF_RECORD IMPLEMENTATION-DEFINED
 * 00-04         05            06             07-08
 *   CHARACTER_CODING_SCHEME  INDICATOR_COUNT SUBFIELD_CODE_LENGTH
 *   09                       10              11
 *     BASE_ADDRESS_OF_DATA  IMPLEMENTATION-DEFINED  ENTRY_MAP
 *     12-16                 17-19                   20-23
 * </pre>
 * <pre>
 * <P>CNMARC Leader</p>
 * 记录长度 记录状态 记录类型 书目级别 层次级别代码 未定义 指示符长度
 * 00-04    05       06       07       08           09     10
 * 子字段指示符长度 数据基地址 编目等级 著录格式 未定义 地址目次区款目结构
 * 11               12-16      17       18       19     20-23
 * </pre>
 */
public class Leader {

    /** The base address of data. */
    private int baseAddressOfData;

    /** The length of the record */
    private int recordLength = 0;

    /** number format for both record length and base address of data */
    DecimalFormat df = new DecimalFormat("00000");

    /**
     * <p>Leader constructor</p>
     *
     * @param ldr string contains leader information
     */
    public Leader(String ldr) {
        parse(ldr);
    }

    /**
     * <p>Parse leader information from a string object.</p>
     *
     * @param ldr the leader string
     */
    private void parse(String ldr) {
        setRecordLength(Integer.parseInt(ldr.substring(00, 05)));
        setBaseAddressOfData(Integer.parseInt(ldr.substring(12, 17)));
    }

    /**
     * <p>Registers the base address of data (positions 12-16).</p>
     *
     * @param baseAddressOfData integer representing the
     *                          base address of data
     */
    private void setBaseAddressOfData(int baseAddressOfData) {
        this.baseAddressOfData = baseAddressOfData;
    }

    /**
     * <p>Returns the base address of data (positions 12-16).</p>
     *
     * @return <code>int</code> - the base address of data
     */
    public int getBaseAddressOfData() {
        return baseAddressOfData;
    }

    /**
     * <p>Registers the logical record length (positions 00-04).  </p>
     *
     * @param recordLength integer representing the record length
     */
    private void setRecordLength(int recordLength) {
        this.recordLength = recordLength;
    }

    /**
     * <p>Returns the logical record length (positions 00-04).</p>
     *
     * @return <code>int</code> - the record length
     */
    public int getRecordLength() {
        return recordLength;
    }

}
